/*     */ package com.zimbra.cs.dav.service;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavContext.RequestProp;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.DavProtocol;
/*     */ import com.zimbra.cs.dav.DomUtil;
/*     */ import com.zimbra.cs.dav.property.ResourceProperty;
/*     */ import com.zimbra.cs.dav.resource.DavResource;
/*     */ import java.io.IOException;
/*     */ import java.io.OutputStream;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DavResponse
/*     */ {
/*  53 */   public static Map<Integer, String> sStatusTextMap = new HashMap();
/*     */   
/*  55 */   static { sStatusTextMap.put(Integer.valueOf(100), "HTTP/1.1 100 Continue");
/*  56 */     sStatusTextMap.put(Integer.valueOf(101), "HTTP/1.1 101 Switching Protocols");
/*     */     
/*  58 */     sStatusTextMap.put(Integer.valueOf(200), "HTTP/1.1 200 OK");
/*  59 */     sStatusTextMap.put(Integer.valueOf(201), "HTTP/1.1 201 Created");
/*  60 */     sStatusTextMap.put(Integer.valueOf(202), "HTTP/1.1 202 Accepted");
/*  61 */     sStatusTextMap.put(Integer.valueOf(203), "HTTP/1.1 203 Non-Authoritative Information");
/*  62 */     sStatusTextMap.put(Integer.valueOf(204), "HTTP/1.1 204 No Content");
/*  63 */     sStatusTextMap.put(Integer.valueOf(205), "HTTP/1.1 205 Reset Content");
/*  64 */     sStatusTextMap.put(Integer.valueOf(206), "HTTP/1.1 206 Partial Content");
/*     */     
/*  66 */     sStatusTextMap.put(Integer.valueOf(300), "HTTP/1.1 300 Multiple Choices");
/*  67 */     sStatusTextMap.put(Integer.valueOf(301), "HTTP/1.1 301 Moved Permanently");
/*  68 */     sStatusTextMap.put(Integer.valueOf(302), "HTTP/1.1 302 Found");
/*  69 */     sStatusTextMap.put(Integer.valueOf(303), "HTTP/1.1 303 See Other");
/*  70 */     sStatusTextMap.put(Integer.valueOf(304), "HTTP/1.1 304 Not Modified");
/*  71 */     sStatusTextMap.put(Integer.valueOf(305), "HTTP/1.1 305 Use Proxy");
/*  72 */     sStatusTextMap.put(Integer.valueOf(307), "HTTP/1.1 307 Temporary Redirect");
/*     */     
/*  74 */     sStatusTextMap.put(Integer.valueOf(400), "HTTP/1.1 400 Bad Request");
/*  75 */     sStatusTextMap.put(Integer.valueOf(401), "HTTP/1.1 401 Unauthorized");
/*  76 */     sStatusTextMap.put(Integer.valueOf(402), "HTTP/1.1 402 Payment Required");
/*  77 */     sStatusTextMap.put(Integer.valueOf(403), "HTTP/1.1 403 Forbidden");
/*  78 */     sStatusTextMap.put(Integer.valueOf(404), "HTTP/1.1 404 Not Found");
/*  79 */     sStatusTextMap.put(Integer.valueOf(405), "HTTP/1.1 405 Method Not Allowed");
/*  80 */     sStatusTextMap.put(Integer.valueOf(406), "HTTP/1.1 406 Not Acceptable");
/*  81 */     sStatusTextMap.put(Integer.valueOf(407), "HTTP/1.1 407 Proxy Authentication Required");
/*  82 */     sStatusTextMap.put(Integer.valueOf(408), "HTTP/1.1 408 Request Time-out");
/*  83 */     sStatusTextMap.put(Integer.valueOf(409), "HTTP/1.1 409 Conflict");
/*  84 */     sStatusTextMap.put(Integer.valueOf(410), "HTTP/1.1 410 Gone");
/*  85 */     sStatusTextMap.put(Integer.valueOf(411), "HTTP/1.1 411 Length Required");
/*  86 */     sStatusTextMap.put(Integer.valueOf(412), "HTTP/1.1 412 Precondition Failed");
/*  87 */     sStatusTextMap.put(Integer.valueOf(413), "HTTP/1.1 413 Reques Entity Too Large");
/*  88 */     sStatusTextMap.put(Integer.valueOf(414), "HTTP/1.1 414 Request-URI Too Large");
/*  89 */     sStatusTextMap.put(Integer.valueOf(415), "HTTP/1.1 415 Unsupported Media Type");
/*  90 */     sStatusTextMap.put(Integer.valueOf(416), "HTTP/1.1 416 Requested range not satisfiable");
/*  91 */     sStatusTextMap.put(Integer.valueOf(417), "HTTP/1.1 417 Expectation Failed");
/*     */     
/*  93 */     sStatusTextMap.put(Integer.valueOf(500), "HTTP/1.1 500 Internal Server Error");
/*  94 */     sStatusTextMap.put(Integer.valueOf(501), "HTTP/1.1 501 Not Implemented");
/*  95 */     sStatusTextMap.put(Integer.valueOf(502), "HTTP/1.1 502 Bad Gateway");
/*  96 */     sStatusTextMap.put(Integer.valueOf(503), "HTTP/1.1 503 Service Unavailable");
/*  97 */     sStatusTextMap.put(Integer.valueOf(504), "HTTP/1.1 504 Gateway Time-out");
/*  98 */     sStatusTextMap.put(Integer.valueOf(505), "HTTP/1.1 505 HTTP Version not supported");
/*     */     
/*     */ 
/* 101 */     sStatusTextMap.put(Integer.valueOf(102), "HTTP/1.1 102 Processing");
/* 102 */     sStatusTextMap.put(Integer.valueOf(207), "HTTP/1.1 207 Multi-Status");
/* 103 */     sStatusTextMap.put(Integer.valueOf(422), "HTTP/1.1 422 Unprocessable Entity");
/* 104 */     sStatusTextMap.put(Integer.valueOf(423), "HTTP/1.1 423 Locked");
/* 105 */     sStatusTextMap.put(Integer.valueOf(424), "HTTP/1.1 424 Failed Dependency");
/* 106 */     sStatusTextMap.put(Integer.valueOf(507), "HTTP/1.1 507 Insufficient Storage");
/*     */   }
/*     */   
/*     */   private Document mResponse;
/*     */   public DavResponse()
/*     */   {
/* 112 */     this.mResponse = DocumentHelper.createDocument();
/*     */   }
/*     */   
/*     */   public Element getTop(QName topName) {
/* 116 */     Element top = this.mResponse.getRootElement();
/* 117 */     if (top == null)
/* 118 */       top = this.mResponse.addElement(topName);
/* 119 */     return top;
/*     */   }
/*     */   
/*     */   public void addProperty(DavContext ctxt, ResourceProperty prop) throws DavException {
/* 123 */     Element top = this.mResponse.addElement(DavElements.E_PROP);
/* 124 */     prop.toElement(ctxt, top, false);
/*     */   }
/*     */   
/*     */ 
/*     */   public void addResource(DavContext ctxt, DavResource rs, DavContext.RequestProp props, boolean includeChildren)
/*     */     throws DavException
/*     */   {
/* 131 */     ctxt.setStatus(207);
/* 132 */     ctxt.setDavCompliance(DavProtocol.getComplianceString(rs.getComplianceList()));
/* 133 */     addResourceTo(ctxt, rs, props, includeChildren);
/*     */     
/* 135 */     if ((rs.isCollection()) && (includeChildren))
/* 136 */       for (DavResource child : rs.getChildren(ctxt))
/* 137 */         addResource(ctxt, child, props, includeChildren);
/*     */   }
/*     */   
/*     */   public void addResourceTo(DavContext ctxt, DavResource rs, DavContext.RequestProp props, boolean includeChildren) throws DavException {
/* 141 */     if (!rs.isValid()) {
/* 142 */       addStatus(ctxt, rs.getUri(), 404);
/* 143 */       return;
/*     */     }
/* 145 */     Element top = getTop(DavElements.E_MULTISTATUS).addElement(DavElements.E_RESPONSE);
/* 146 */     rs.getProperty(DavElements.E_HREF).toElement(ctxt, top, false);
/*     */     
/*     */     Collection<QName> propNames;
/*     */     Collection<QName> propNames;
/* 150 */     if (props.isAllProp()) {
/* 151 */       propNames = rs.getAllPropertyNames();
/*     */     } else {
/* 153 */       propNames = props.getProps();
/*     */     }
/* 155 */     PropStat propstat = new PropStat();
/* 156 */     Map<QName, DavException> errPropMap = props.getErrProps();
/* 157 */     for (QName name : propNames) {
/* 158 */       ResourceProperty prop = rs.getProperty(name, props);
/* 159 */       if (errPropMap.containsKey(name)) {
/* 160 */         DavException ex = (DavException)errPropMap.get(name);
/* 161 */         propstat.add(name, null, ex.getStatus());
/* 162 */       } else if (prop == null) {
/* 163 */         if (!ctxt.isBrief())
/* 164 */           propstat.add(name, null, 404);
/*     */       } else {
/* 166 */         propstat.add(prop);
/*     */       }
/*     */     }
/*     */     
/* 170 */     propstat.toResponse(ctxt, top, props.isNameOnly());
/*     */   }
/*     */   
/*     */   public void addResources(DavContext ctxt, Collection<DavResource> rss, DavContext.RequestProp props) throws DavException {
/* 174 */     ctxt.setStatus(207);
/* 175 */     boolean first = true;
/* 176 */     for (DavResource rs : rss) {
/* 177 */       if (first)
/* 178 */         ctxt.setDavCompliance(DavProtocol.getComplianceString(rs.getComplianceList()));
/* 179 */       addResourceTo(ctxt, rs, props, false);
/* 180 */       first = false;
/*     */     }
/*     */   }
/*     */   
/*     */   public void addStatus(DavContext ctxt, String href, int status) {
/* 185 */     ctxt.setStatus(207);
/* 186 */     Element resp = getTop(DavElements.E_MULTISTATUS).addElement(DavElements.E_RESPONSE);
/* 187 */     resp.addElement(DavElements.E_HREF).setText(href);
/* 188 */     resp.addElement(DavElements.E_STATUS).setText((String)sStatusTextMap.get(Integer.valueOf(status)));
/*     */   }
/*     */   
/*     */   public void createResponse(DavContext ctxt) {
/* 192 */     ctxt.setStatus(207);
/* 193 */     getTop(DavElements.E_MULTISTATUS);
/*     */   }
/*     */   
/*     */   public void writeTo(OutputStream out) throws IOException
/*     */   {
/* 198 */     if (ZimbraLog.dav.isDebugEnabled())
/* 199 */       ZimbraLog.dav.debug("RESPONSE:\n" + new String(DomUtil.getBytes(this.mResponse), "UTF-8"));
/* 200 */     DomUtil.writeDocumentToStream(this.mResponse, out);
/*     */   }
/*     */   
/*     */   public static class PropStat {
/*     */     private HashMap<Integer, Element> mMap;
/*     */     private ArrayList<ResourceProperty> mProps;
/*     */     
/* 207 */     public PropStat() { this.mProps = new ArrayList();
/* 208 */       this.mMap = new HashMap(); }
/*     */     
/*     */     public void toResponse(DavContext ctxt, Element response, boolean nameOnly) { Element propElem;
/* 211 */       if (!this.mProps.isEmpty()) {
/* 212 */         propElem = findProp(200);
/* 213 */         for (ResourceProperty prop : this.mProps)
/* 214 */           prop.toElement(ctxt, propElem, nameOnly);
/*     */       }
/* 216 */       for (Integer code : this.mMap.keySet())
/* 217 */         response.add((Element)this.mMap.get(code));
/*     */     }
/*     */     
/* 220 */     public void add(ResourceProperty prop) { this.mProps.add(prop); }
/*     */     
/*     */     public void add(QName name, String msg, int code) {
/* 223 */       Element e = findProp(code).addElement(name);
/* 224 */       if (msg != null)
/* 225 */         e.setText(msg);
/*     */     }
/*     */     
/* 228 */     public void add(QName name, Element value) { value.detach();
/* 229 */       Element prop = findProp(200);
/* 230 */       Element e = null;
/* 231 */       for (Object obj : prop.elements()) {
/* 232 */         if (((obj instanceof Element)) && (((Element)obj).getQName().equals(name))) {
/* 233 */           e = (Element)obj;
/* 234 */           break;
/*     */         }
/*     */       }
/* 237 */       if (e == null)
/* 238 */         e = prop.addElement(name);
/* 239 */       e.add(value);
/*     */     }
/*     */     
/* 242 */     private Element findProp(int status) { Element propstat = findPropstat(status);
/* 243 */       return propstat.element(DavElements.E_PROP);
/*     */     }
/*     */     
/* 246 */     private Element findPropstat(int status) { Element propStat = (Element)this.mMap.get(Integer.valueOf(status));
/* 247 */       if (propStat == null) {
/* 248 */         propStat = DocumentHelper.createElement(DavElements.E_PROPSTAT);
/* 249 */         propStat.addElement(DavElements.E_STATUS).setText((String)DavResponse.sStatusTextMap.get(Integer.valueOf(status)));
/* 250 */         propStat.addElement(DavElements.E_PROP);
/* 251 */         this.mMap.put(Integer.valueOf(status), propStat);
/*     */       }
/* 253 */       return propStat;
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/service/DavResponse.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */